﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain;
using Service.Interface;
using Dao.Interface;

namespace Service
{
    public class NoteBookService : Service<NoteBook>, INoteBookService
    {
        public IUserInfoRepository UserInfoRepository;
        public IAreaRepository AreaRepository;
        public IPageRepository PageRepository;

        /// <summary>
        /// 添加用户默认笔记本、默认分区
        /// </summary>
        /// <param name="userInfo"></param>
        public void SaveNoteBookAndArea(UserInfo userInfo)
        {
            userInfo.Activate = true;
            this.UserInfoRepository.Save(userInfo);

            //添加用户默认笔记本
            NoteBook notebook = new NoteBook();
            notebook.Name = userInfo.Name + "的笔记本";
            notebook.MarkDelete = false;
            notebook.Share = false;
            notebook.UserInfo = userInfo;
            ((INoteBookRepository)(this.CurrentRepository)).Save(notebook);
            //添加默认分区
            Area area = new Area();
            area.Name = "默认分类";
            area.MarkDelete = false;
            area.Share = false;
            area.PageCount = 0;
            area.PageSortType = "CreateTime";
            area.NoteBook = notebook;
            this.AreaRepository.Save(area);
            //更新分区排列字符串
            notebook.AreaSort = area.ID + "-";
            ((INoteBookRepository)(this.CurrentRepository)).Update(notebook);
            userInfo.NoteBookSort = notebook.ID + "-";
            this.UserInfoRepository.Update(userInfo);
        }

        /// <summary>
        /// 添加用户默认笔记本、默认分区
        /// </summary>
        /// <param name="notebookName">笔记本名称</param>
        /// <param name="userID">用户ID</param>
        public NoteBook SaveNoteBookAndArea(string notebookName, int userID)
        {
            //添加用户默认笔记本
            NoteBook notebook = new NoteBook();
            notebook.Name = notebookName;
            notebook.MarkDelete = false;
            notebook.Share = false;
            notebook.UserInfo = new UserInfo { ID = userID };
            ((INoteBookRepository)(this.CurrentRepository)).Save(notebook);
            //添加默认分区
            Area area = new Area();
            area.Name = "默认分类";
            area.MarkDelete = false;
            area.Share = false;
            area.PageCount = 0;
            area.PageSortType = "CreateTime";
            area.NoteBook = notebook;
            this.AreaRepository.Save(area);
            //更新分区排列字符串
            notebook.AreaSort = area.ID + "-";
            ((INoteBookRepository)(this.CurrentRepository)).Update(notebook);

            return notebook;
        }

        /// <summary>
        /// 获取笔记本集合
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="markDelete">是否删除</param>
        /// <returns></returns>
        public IList<NoteBook> LoadAllNoteBook(object userID,bool markDelete)
        {
            if (userID == null)
            {
                return null;
            }
            return ((INoteBookRepository)(this.CurrentRepository)).LoadAllNoteBook(userID, markDelete);
        }

        /// <summary>
        /// 获取共享笔记本集合
        /// </summary>
        /// <param name="share">是否共享</param>
        /// <returns></returns>
        public IList<NoteBook> LoadAllShareNoteBook()
        {
            return ((INoteBookRepository)(this.CurrentRepository)).LoadAllShareNoteBook();
        }

        /// <summary>
        /// 获取共享笔记本集合
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="share">是否共享</param>
        /// <returns></returns>
        public IList<NoteBook> LoadAllShareNoteBook(object userID, bool share)
        {
            if (userID == null)
            {
                return null;
            }
            return ((INoteBookRepository)(this.CurrentRepository)).LoadAllNoteBook(userID, share);
        }

        /// <summary>
        /// 更新笔记本是否扔进垃圾箱
        /// </summary>
        /// <param name="notebookID">笔记本ID</param>
        /// <param name="delete">是否删除</param>
        public void UpdateNoteBookMarkDelete(object notebookID, bool delete)
        {
            if (notebookID == null)
            {
                return;
            }
            NoteBook notebook = ((INoteBookRepository)(this.CurrentRepository)).Get(notebookID);
            notebook.MarkDelete = delete;
            ((INoteBookRepository)(this.CurrentRepository)).Update(notebook);
        }

        /// <summary>
        /// 重写删除，删除笔记本，分区，分区中的页
        /// </summary>
        /// <param name="id">笔记本ID</param>
        public override void Delete(object id)
        {
            if (id == null)
            {
                return;
            }
            NoteBook notebook = ((INoteBookRepository)(this.CurrentRepository)).Get(id);
            foreach (Area area in notebook.Areas)
            {
                //删除分区中的页
                PageRepository.Delete(area.Pages);
                //删除分区
                AreaRepository.Delete(area);
            }
            ((INoteBookRepository)(this.CurrentRepository)).Delete(notebook);
        }

        ///// <summary>
        ///// 删除垃圾箱中所有数据
        ///// </summary>
        ///// <param name="userID">用户ID</param>
        public void DeleteAllMarkDelete(object userID)
        {
            if (userID == null)
            {
                return;
            }
            //}
            ////获取用户笔记本集合
            //IList<NoteBook> notebookList = ((INoteBookRepository)(this.CurrentRepository)).LoadAllNoteBook(userID);
            //foreach (NoteBook notebook in notebookList)
            //{
            //    if (notebook.MarkDelete == true)//笔记本标记为删除,则删除该笔记本、分区、页
            //    {
            //        foreach (Area area in notebook.Areas)
            //        {
            //            foreach (Page page in area.Pages)
            //            {
            //                PageRepository.Delete(page);
            //            }
            //            AreaRepository.Delete(area);
            //        }
            //        ((INoteBookRepository)(this.CurrentRepository)).Delete(notebook);
            //    }
            //    else//笔记本没有被标记为删除
            //    {
            //        foreach (Area area in notebook.Areas)
            //        {
            //            if (area.MarkDelete == true)//分区标记为删除，则删除该分区、页
            //            {
            //                PageRepository.Delete(area.Pages);
            //                AreaRepository.Delete(area);
            //            }
            //            else//分区没有被标记为删除，则遍历分区中的页。若页被标记为删除，则删除
            //            {
            //                foreach (Page page in area.Pages)
            //                {
            //                    if (page.MarkDelete == true)
            //                    {
            //                        PageRepository.Delete(page);
            //                    }
            //                }
            //            }
            //        }
            //    }

            IList<Page> pageList = this.PageRepository.LoadAllPage(userID, true);
            this.PageRepository.Delete(pageList);

            //获取用户笔记本集合
            IList<NoteBook> notebookList = ((INoteBookRepository)(this.CurrentRepository)).LoadAllNoteBook(userID);
            foreach (NoteBook notebook in notebookList)
            {
                if (notebook.MarkDelete == true)
                {
                    foreach (Area area in notebook.Areas)
                    {
                        foreach (Page page in area.Pages)
                        {
                            this.PageRepository.Delete(page);
                        }
                        AreaRepository.Delete(area);
                    }
                    ((INoteBookRepository)(this.CurrentRepository)).Delete(notebook);
                }
                else
                {
                    foreach (Area area in notebook.Areas)
                    {
                        if (area.MarkDelete == true)
                        {
                            foreach (Page page in area.Pages)
                            {
                                this.PageRepository.Delete(page);
                            }
                            AreaRepository.Delete(area);
                        }
                    }
                }
            }

        }
           
        
        /// <summary>
        /// 还原垃圾箱
        /// </summary>
        /// <param name="userID">用户ID</param>
        public void UpdateAllMarkDeleteEqualsTrue(object userID)
        {
            if (userID == null)
            {
                return;
            }
            //还原笔记本
            IList<NoteBook> notebookList = ((INoteBookRepository)(this.CurrentRepository)).LoadAllNoteBook(userID, true);
            foreach (NoteBook notebook in notebookList)
            {
                notebook.MarkDelete = false;
                ((INoteBookRepository)(this.CurrentRepository)).Save(notebook);
            }
            //还原分区
            IList<Area> areaList = this.AreaRepository.LoadAllArea(userID, true);
            foreach (Area area in areaList)
            {
                area.MarkDelete = false;
                this.AreaRepository.Save(area);
            }
            //还原页
            IList<Page> pageList = this.PageRepository.LoadAllPage(userID, true);
            foreach (Page page in pageList)
            {
                page.MarkDelete = false;
                this.PageRepository.Save(page);
            }
        }

        /// <summary>
        /// 共享笔记本
        /// </summary>
        /// <param name="notebookID">笔记本ID</param>
        /// <param name="share">是否共享</param>
        public void UpdateNoteBookShare(object notebookID, bool share)
        {
            if (notebookID == null)
            {
                return;
            }
            NoteBook notebook = ((INoteBookRepository)(this.CurrentRepository)).Get(notebookID);
            notebook.Share = share;
            ((INoteBookRepository)(this.CurrentRepository)).Update(notebook);
            foreach (Area area in notebook.Areas)
            {
                area.Share = share;
                this.AreaRepository.Update(area);
                foreach (Page page in area.Pages)
                {
                    page.Share = share;
                    this.PageRepository.Update(page);
                }
            }
        }

    }
}
